package game.gamestate.debugroom.ui;

import common.Coordinate;
import common.Rectangle;
import engine.input.KeyInfo;
import engine.input.KeyboardHandler_I;
import engine.video.blit.BlitMode_E;
import engine.video.palette.Color_E;

import game.entity.ui.UI_Entity;
import game.gamestate.GameState;
import game.scene.Layer_E;
import game.scene.sceneobject.Rectangle_SO;
import game.scene.sceneobject.SceneObject_A;

import java.awt.event.KeyEvent;

public class EditCursor extends UI_Entity implements KeyboardHandler_I {
	public EditCursor() {
		super("edit_cursor");
		Location = new Coordinate();
		_StepX = 8;
		_StepY = 8;
	}
	private final int _StepX;
	private final int _StepY;
	
	private SceneObject_A _Cursor;
	
	public Coordinate Location;
	
	@Override
	public boolean create(GameState gameState) {
		Rectangle_SO cursor = new Rectangle_SO();
		cursor.setBoundry(new Rectangle(-4, -4, 8, 8));
		cursor.Color = Color_E.MAGENTA.Value;
		cursor.Mode = BlitMode_E.XOR;
		_Cursor = new Rectangle_SO();
		_Cursor.setLocation(Location);
		gameState.Scene.getLayer(Layer_E.UI).insert(_Cursor);
		
		gameState.Keyboard.add(this);
		return false;
	}

	@Override
	public boolean think(GameState gameState, int time) {
		return false;
	}

	@Override
	public boolean update(GameState gameState, int time) {
		_Cursor.setLocation(Location);
		return false;
	}

	@Override
	public boolean destroy(GameState gameState) {
		gameState.Scene.getLayer(Layer_E.UI).remove(_Cursor);
		gameState.Keyboard.remove(this);
		return false;
	}

	@Override
	public boolean handleKeyboard(KeyInfo keyInfo) {
		switch(keyInfo.VKey) {
			case KeyEvent.VK_UP:
				if(keyInfo.Pressed == true) {
					Location.translate(0, -_StepY);
				}
				break;
			case KeyEvent.VK_DOWN:
				if(keyInfo.Pressed == true) {
					Location.translate(0, _StepY);
				}
				break;
			case KeyEvent.VK_LEFT:
				if(keyInfo.Pressed == true) {
					Location.translate(-_StepX, 0);
				}
				break;
			case KeyEvent.VK_RIGHT:
				if(keyInfo.Pressed == true) {
					Location.translate(_StepX, 0);
				}
				break;
			default:
				return false;
		}
		return true;
	}
}